package com.vaadin.thomas.contenttool.ui;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import com.vaadin.thomas.contenttool.data.Localization;
import com.vaadin.thomas.contenttool.data.Value;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;

public class PreviousValuesWindow extends Window {

	private static final long serialVersionUID = 7703684866784123102L;

	private final Value value;
	private final Locale locale;
	private final boolean editable;

	public PreviousValuesWindow(Value v, Locale l, boolean editable) {

		value = v;
		locale = l;
		this.editable = editable;

		setModal(true);
		center();
		setHeight(50, UNITS_PERCENTAGE);
		setWidth(50, UNITS_PERCENTAGE);
		addStyleName("previousvalues");

		setCaption(AdminMainLayout.ct().get(
				"contenttool.content.oldversions.caption")
				+ " " + value.getKey());
		setIcon(Util.getResource("flags/" + l.getLanguage()));

		build();
	}

	private void build() {
		GridLayout content = new GridLayout(3, 1);
		content.setWidth(100, UNITS_PERCENTAGE);
		content.setMargin(true);
		content.setSpacing(true);
		content.setColumnExpandRatio(0, 1);
		setContent(content);

		List<Value> versions = AdminMainLayout.ct().getModel()
				.getAllVersions(value.getKey());
		DateFormat df = new SimpleDateFormat("dd.MM.yyyy");
		for (final Value val : versions) {

			// the version might not have this exact language
			if (!val.getLocalizations().containsKey(locale)) {
				continue;
			}

			boolean isPreviewVersion = AdminMainLayout.ct().getModel()
					.isPartOfPreview(val);

			for (final Localization loc : val.getLocalizations().get(locale)) {
				Label label = new Label();
				label.setWidth(100, UNITS_PERCENTAGE);
				label.addStyleName("content");
				label.setValue(parseToHTML(loc.getValue()));
				label.setContentMode(Label.CONTENT_XHTML);
				label.setCaption(Util.parseVersion(val.getVersion()));
				addComponent(label);

				Component copy;
				if (isPreviewVersion || !editable) {
					copy = new Label();
				} else {
					copy = new Button(AdminMainLayout.ct().get(
							"contenttool.content.oldversions.copy"));
					((Button) copy).addListener(new Button.ClickListener() {

						private static final long serialVersionUID = 5691249675040856152L;

						@Override
						public void buttonClick(ClickEvent event) {
							// copy existing to new
							AdminMainLayout
									.ct()
									.getModel()
									.setValue(value.getKey(), locale,
											loc.getValue(), loc.getStartDate());
							build();
						}
					});
				}
				addComponent(copy);
				content.setComponentAlignment(copy, Alignment.MIDDLE_CENTER);

				Label committed = new Label();
				committed.setSizeUndefined();
				if (val.getVersion().isPreview()) {
					committed.setValue("-");
				} else {
					String commitedBy = AdminMainLayout
							.ct()
							.conf()
							.getUserNameForAdminUI(
									val.getVersion().getCommitterId())
							+ ", "
							+ df.format(val.getVersion().getCommitDate());
					committed.setValue(commitedBy);
				}
				addComponent(committed);
				content.setComponentAlignment(committed,
						Alignment.MIDDLE_CENTER);
			}
		}
	}

	private String parseToHTML(String s) {
		return s.replaceAll("\r\n", "<br/>").replaceAll("\r", "<br/>")
				.replaceAll("\n", "<br/>");
	}

}
